using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Imagination.Partitioning
{
    public class BoxPartitioner : Partitioner
    {
        private BoxZone rootZone;

        public BoxZone RootZone
        {
            get { return rootZone; }
        }
        public BoxPartitioner()
            : base()
        {
            rootZone = new BoxZone();
        }

        public override void CheckCollisions(Imagination.Collisions.CollisionDetector detector)
        {
            rootZone.CheckCollisions(detector);
        }

        public override void CheckPartitioning(Thing thing)
        {
            BoxZone zone = rootZone.LocateZone(thing);
            if (zone != thing.Zone)
            {
                if(thing.Zone != null)
                    thing.Zone.RemoveThing(thing);
                zone.AddThing(thing);
            }
        }

        public override List<Zone> GetVisibleZones(Imagination.Display.Camera camera)
        {
            List<Zone> visibleZones = new List<Zone>();
            foreach (BoxZone zone in rootZone.Zones)
            {
                if(camera.Frustum.Contains(zone.Boundary) != ContainmentType.Disjoint)
                    visibleZones.Add(zone);
            }
            return visibleZones;
        }

        public override void Clear()
        {
            rootZone.ClearThings();
        }

        public override string ToString()
        {
            return "BoxPartitioner: " + rootZone.ToString();
        }
    }
}
